package com.github.bobjoy.java.thread.concurrent.atomic;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerDemo {

    private static AtomicInteger ai = new AtomicInteger(0);
    private static Integer i = 0;

    public static void main(String[] args) throws InterruptedException {

        System.out.println("更新前：ai=" + AtomicIntegerDemo.ai.get() + ", i=" + AtomicIntegerDemo.i);
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10000; i++) {
                AtomicIntegerDemo.ai.getAndIncrement();
                AtomicIntegerDemo.i++;
            }
        });

        Thread t2 = new Thread(() -> {
            for(int i = 0; i < 10000; i++) {
                AtomicIntegerDemo.ai.getAndDecrement();
                AtomicIntegerDemo.i--;
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("更新后：ai=" + AtomicIntegerDemo.ai.get() + ", i=" + AtomicIntegerDemo.i);
    }
}
